﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using System.Collections;
using NHibernate.Transform;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.Wap.Code
{
    public partial class WebMemberPageBase : System.Web.UI.Page
    {

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
           // Session[GlobalSessionString.CurrentMemberAccount] = "13434343434";

            //保存ASP.NET验证数据
          //  new UserKit().SaveTicket("13434343434", "1"); 
            if (CurrentMemberAccount == null)
            {
                Server.Transfer("/MemberUser/Login.aspx");
            }
        }

        /// <summary>
        /// 当前用户帐号
        /// </summary>
        public string CurrentMemberAccount
        {
            get
            {
                return new UserKit().GetTicketName(Request);
                //return Session[GlobalSessionString.CurrentMemberAccount] as string;
            }
        }

        public void Logout()
        {
            System.Web.HttpContext.Current.Session[GlobalSessionString.CurrentMemberAccount] = null;
        }

        public MemberInfo CurrentMember
        {
            get
            {
                if (_member == null)
                {
                    using (ISession session = SessionFactoryHelper.CreateSession())
                    {
                        _member = session.CreateQuery("from MemberInfo where AccountId=:acc")
                            .SetParameter("acc", CurrentMemberAccount)
                            .UniqueResult<MemberInfo>();
                    }
                }
                return _member;
            }
        }

        /// <summary>
        /// 获取view_member_info的一条信息
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        protected IDictionary LoadMemberDictionary(string account)
        {

            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                return session.CreateSQLQuery("select * from view_member_info where account_id=:acc")
                    .SetParameter("acc", account)
                    .SetResultTransformer(Transformers.AliasToEntityMap)
                    .UniqueResult<IDictionary>();
            }

        }

        /// <summary>
        /// 获取用户头像
        /// </summary>
        /// <param name="accountJson"></param>
        /// <param name="sex"></param>
        /// <returns></returns>
        protected string GetMemberPhoto(object json)
        {
            string accountJson = json as string;
            string result = FileKit.FirstFilePath(accountJson);
            string path = Server.MapPath("~/");
            if (!string.IsNullOrEmpty(result) && new System.IO.FileInfo(path + result).Exists)
            {
                string thumb = result + ".thumb.jpg";

                //缩略图不存在，则创建
                if (!new System.IO.FileInfo(path + thumb).Exists)
                {
                    // ImageUtil.MakeThumbnail(path + result, path + thumb, 120, 120, "HW", "JPG");
                }
                result = thumb;
            }
            else
            {
                result = "/Wap/images/head.gif";
            }
            return result;
        }

        protected MemberInfo _member = null;
    }
}